package com.example.reagent_management.ui.operatedialog.repository

import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.example.reagent_management.room.dao.HistoryDao
import com.example.reagent_management.room.entity.HistoryInfo
import com.example.reagent_management.ui.activity.pojo.RemindDateAndCount
import com.example.reagent_management.ui.statistics.pojo.OperateTimesAndNum
import com.example.reagent_management.ui.statistics.pojo.ReagentNameAndNum
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject
import javax.inject.Singleton

//记录信息唯一数据源
@Singleton
class HistoryRepository @Inject constructor(private val historyDao: HistoryDao) {

    suspend fun insertHistory(historyInfo: HistoryInfo): Long {
        return historyDao.insertHistory(historyInfo)
    }

    fun getAllHistory(): Flow<PagingData<HistoryInfo>> {
        return Pager(config = PagingConfig(
            enablePlaceholders = false,
            pageSize = 45,
            maxSize = 200
        ),
            pagingSourceFactory = { historyDao.getAllHistory() }
        ).flow
    }

    fun queryById(id: Long): HistoryInfo {
        return historyDao.queryById(id)
    }

    fun deleteById(id: Long):Int{
        return historyDao.deleteById(id)
    }

    fun updateCounts(batch:String,reagent_id: Long,counts:Long) {
        historyDao.updateCounts(batch,reagent_id,counts)
    }
    /*
    * 获取试剂名出库排名
    * */
    fun getNameAndNum(startMonth:Long,endMonth: Long): Flow<List<ReagentNameAndNum>>{
        return  historyDao.getNameAndNum(startMonth,endMonth)
    }

    /*
    * 近24小时 出库
    * */
    fun  getHourOutStore():Flow<List<OperateTimesAndNum>>{
        return historyDao.getHourOutStore()
    }

    /*
   * 近24小时 出库
   * */
    fun  getHourInStore():Flow<List<OperateTimesAndNum>>{
        return historyDao.getHourInStore()
    }
    /*
    *近7天
    * */
    fun getWeekStore(operateType:Boolean):Flow<List<OperateTimesAndNum>>{
        return historyDao.getWeekStore(operateType)
    }

    /**
     * 清理
     */
    fun cleanHistory(){
        historyDao.cleanHistory()
    }

    fun updateReagent(id: Long,reagentName:String):Int{
        return historyDao.updateReagent(id, reagentName)
    }
}